<?php
#include_once('../model/Connection.php');
include_once('../model/Utils.php');
include_once('../model/Userinfo.php');
include_once('../sql/sql.php');

$username   = $_POST['username'];
$u_password = $_POST['password'];

$ui = new Userinfo($username, $u_password); //Online Environment
#$ui = new Userinfo("admin", "admin"); //Test Environment

function Login($mysqli, $ui)
{
    global $SQL_LOGIN;
    if ($stmt = $mysqli->prepare($SQL_LOGIN)) {
        $stmt->bind_param("s", $ui->getUsername());
        $stmt->execute();
        $stmt->bind_result($password, $authz);
        if (!$stmt->fetch()) {
            $login_flag = 0;
        }
    #    printf("%s's password is %s, authz is %s\n", $username, $password, $authz);
        printf("%d\n", $stmt->num_rows);
        $stmt->close();
    } else {
        return FALSE;
    }
    
    
    if (password_verify($ui->getPassword(), $password)) {
        echo "Login OK!\n";
        $login_flag = 1;
    } else {
        echo "Login Failed!\n";
        $login_flag = -1;
    }
    return $login_flag;
    #    printf("%s's password is %s, authz is %s\n", $username, $password, $authz);
}
#echo Login($mysqli, $ui);

function Register($mysqli, $ui)
{
    global $SQL_REGISTER;
    if ($stmt = $mysqli->prepare($SQL_REGISTER)) {
        $stmt->bind_param("ssi", $ui->getUsername(),
            $ui->getPassword(), $ui->getAuthz());
        if ($stmt->execute()) {
            return TRUE;
        } else {
            return FALSE;
        }
    } else {
        return FALSE;
    }
}
if ($mysqli) {
    $mysqli->close();
}
?>
